/**
 * 
 */
package group.anglers.om.core.persistence.serialization.impl;

import group.anglers.om.core.exceptions.ModelException;
import group.anglers.om.core.persistence.serialization.ModelSerializationArchived;

import java.util.HashMap;
import java.util.Map;

/**
 * @author an.pham
 *
 */
public class LocalModelSerializationArchived implements ModelSerializationArchived {
	private static ModelSerializationArchived instance;
	
	private Map<String, ModelSerialization> pool;
	
	private LocalModelSerializationArchived(){
		pool = new HashMap<String, ModelSerialization>();
	}
	
	public static ModelSerializationArchived getInstance() {
		if(instance == null){
			instance = new LocalModelSerializationArchived();
		}
		return instance;
	}

	@Override
	public void put(Class<?> modelClass) throws ModelException {
		if(!pool.containsKey(modelClass.toString())){
			pool.put(modelClass.getName(), ModelSerializationUtil.serialize(modelClass));
		}
	}

	@Override
	public ModelSerialization get(Class<?> modelClass) throws ModelException {
		ModelSerialization modelSerialization = pool.get(modelClass);
		
		if(modelSerialization == null){
			modelSerialization = ModelSerializationUtil.serialize(modelClass);
			pool.put(modelClass.getName(), modelSerialization);
		}
		
		return modelSerialization;
	}
}
